$(function() {
    //点击切换
    $('.loginBox a').on('click', function() {
        $('#regBox').show()
        $('.loginBox').hide()
    })
    $('#regBox a').on('click', function() {
        $('.loginBox').show()
        $('#regBox').hide()
    })

    //自定义校验规则
    let form = layui.form;
    form.verify({
        username: [
            /^[a-zA-Z0-9]{1,10}$/,
            '用户必须1到10位数字和字母'
        ],
        pwd: [
            /^[a-zA-Z0-9]{6,12}$/,
            '密码必须6到12位数字和字母'
        ],
        rePwd: function(value) {
            let pwd = $('#regBox input[name=password]').val()
            if (pwd !== value) {
                return '两次密码输入不一致'
            }
        }
    })
    let layer = layui.layer
        //注册功能
    $('#formReg').on('submit', function(e) {
            e.preventDefault()
            axios({
                method: 'post',
                url: '/api/reguser',
                data: $(this).serialize()
            }).then(res => {
                if (res.data.status != 0) {
                    return layer.msg(res.data.message)
                }
                layer.msg('恭喜您,注册用户成功!')
                $('#regBox a').click()
                $('#formReg')[0].reset()
            })
        })
        //登录功能
    $('#formLogin').on('submit', function(e) {
        e.preventDefault()
        axios({
            method: 'post',
            url: '/api/login',
            data: $(this).serialize()
        }).then(res => {
            if (res.data.status != 0) {
                return layer.msg(res.data.message)
            }
            layer.msg('恭喜您,登录成功!')
            localStorage.setItem('token', res.data.token)
            location.href = '/index.html'
        })
    })
})